package com.ag.common;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.ag.bean.TbIds;
import com.ag.dao.TbIdsDAO;

/**
 * 获取表主键的业务类
 * 
 * @author 黎肇明
 * 
 */
@Service("iDServiceImpl")
public class IDServiceImpl implements IIDService {
	private static final Log log = LogFactory.getLog(IDServiceImpl.class);
	/**
	 * 主键维护类的DAO
	 */
	@Resource(name="TbIdsDAO")
	private TbIdsDAO iDDao;
 
	/**
	 * 获取表主键的方法 传入参数：主键维护表行数，参照IDUtil类取静态字段值 返回：目标表主键当前值
	 */
	@Override
	public String findTablePK(Long id) throws AgException {
		log.debug("getting table primary key with id: " + id);
		try {
//			TbIds ids = iDDao.findById(id);
			long tmpl=id;
			TbIds ids = iDDao.findById((int)tmpl);
			String tablePK = ids.getIdValues();
			Long tmp = Long.valueOf(tablePK);
			ids.setIdValues((++tmp).toString());
			iDDao.attachDirty(ids);
			return tablePK;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw new AgException(
					"get table primary key failed with id: " + id, re);
		}
	}

}
